package com.example.hello;


import java.sql.SQLOutput;

public class TestVolatile {
    public volatile int inc = 0;
    public volatile int incTest = 100000;

    public void increase() {  //如果去掉关键字 synchronized， 则多线程运行时存在竞争，结果不可预料
        //    public synchronized void increase(){
        inc++;
        incTest --;
    }

    public static void main(String[] args) {
        final TestVolatile test = new TestVolatile();
        for (int i = 0; i < 100; i++) {
            new Thread() {
                public void run() {
                    for (int j = 0; j < 1000; j++) {
                        test.increase();
                    }
                }
            }.start();
        }
        while (Thread.activeCount() > 1) { //这里是个点：再idea环境下，条件是（ >2）； 再dos环境下，条件是 (>1).
            Thread.yield();
        }
        System.out.println(test.inc);//输出 i*j=100*1000 = 10 0000
    }

}

class ThreadCountTest {
    public static void main(String[] args) {
        Thread.currentThread().getThreadGroup().list();
    }
}